<?php

class site_contact_model extends CI_Model
{

    protected $_instance = array(
        'table' => 'site_contact',
        'primaryKey' => 'id',
    );
    public $_data = null;

    public function get_contacts($reload = false)
    {
        $CI = & get_instance();
        $list_contact = null;
        if (!GLOBAL_ENABLED_CACHE || $reload) {
            $query = $this->db->get($this->_instance['table']);
            foreach ($query->result() as $row) {
                $list_contact[] = $row;
            }
            $query->free_result();
            $CI->st_registry->setKey('site/contact/collection', $list_contact, GLOBAL_CACHE_DURATION);
        } else {
            $list_contact = $CI->st_registry->getKey('site/contact/collection');
            if (empty($list_contact)) {
                return $this->get_contacts(true);
            }
        }
        return $list_contact;
    }

    public function is_exists($ip, $email, $phone)
    {
        $query = $this->db->query('SELECT id FROM ' . $this->_instance['table'] . ' WHERE ip = "' . $ip . '" OR email = "'.$email.'" OR phone = "'.$phone.'" LIMIT 0, 1');
        $rs = $query->result();
        $query->free_result();
        if (!$rs || empty($rs->id)) {
            return false;
        }
        return true;

    }

    public function add_contact($data)
    {
        $data = $this->auto_render($data);
        $CI = & get_instance();
        if (empty($data['error'])) {
            $this->db->insert($this->_instance['table'], $data);
            // Update Cache
            $CI->st_registry->deleteKey('site/contact/collection');
        } else {
            throw new Exception($data['error']);
            return false;
        }
        return $this;
    }

    public function update_contact($id, $data)
    {
        $data = $this->auto_render($data);
        $CI = & get_instance();
        if (empty($data['error'])) {
            $this->db->where('id', $id);
            $this->db->update($this->_instance['table'], $data);
            // Update Cache
            $CI->st_registry->deleteKey('site/contact/collection');
        } else {
            throw new Exception($data['error']);
            return false;
        }
        return $this;
    }

    public function load_by_id($id, $reload = false)
    {
        $this->_data = null;
        $CI = & get_instance();
        if (!GLOBAL_ENABLED_CACHE || $reload) {
            $query = $this->db->get_where($this->_instance['table'], array('id' => $id), 1, 0);
            $this->_data = $query->row();
            $query->free_result();
            $CI->st_registry->setKey('site/contact_by_id/' . $id, $this->_data, GLOBAL_CACHE_DURATION);
        } else {
            $this->_data = $CI->st_registry->getKey('site/contact_by_id/' . $id);
            if (empty($this->_data)) {
                $this->_data = $this->load_by_id($id, true);
            }
        }
        return $this->_data;
    }

    public function delete_contact($id)
    {
        $CI = & get_instance();
        $this->db->delete($this->_instance['table'], array('id' => $id));
        // Update Cache
        $CI->st_registry->deleteKey('site/contact/collection');
        $CI->st_registry->deleteKey('site/contact_by_id/' . $id);
        return $this;
    }

    private function auto_render($data)
    {
        return $data;
    }

}

?>